package com.hmall.item.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.MQConstants;
import com.hmall.common.PageDto;
import com.hmall.item.exception.BaseException;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {

    @Resource
    private  ItemMapper itemMapper;
    @Resource
    RabbitTemplate rabbitTemplate;
    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageDto<Item> searchPage(Integer page, Integer size) {
        PageDto<Item> pageDto = new PageDto<>();
        Page<Item> pageItem = new Page<>(page,size);
        Page<Item> itemPage = itemMapper.selectPage(pageItem, null);
        pageDto.setList(itemPage.getRecords());
        pageDto.setTotal(itemPage.getTotal());
        return pageDto;
    }

    /**
     *
     * @param id
     * @return
     */
    @Override
    public Item findById(Long id) {
        return itemMapper.selectById(id);
    }

    /**
     * 新增商品
     * @param item
     */
    @Override
    public void add(Item item) {
        //item.setCreateTime(new Date());
       // item.setUpdateTime(new Date());
        item.setStatus(2);
        itemMapper.insert(item);
    }

    /**
     * 商品上下架
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        Item item = new Item();
        item.setStatus(status);
        item.setId(id);
        itemMapper.updateById(item);
        if (status==1)
        {
            //上架
            rabbitTemplate.convertAndSend(MQConstants.EXCHANGE_NAME,MQConstants.ROUTING_KEY_INSERT,id);
        }else  if (status==2)
        {
            //下架
            rabbitTemplate.convertAndSend(MQConstants.EXCHANGE_NAME,MQConstants.ROUTING_KEY_DELETE,id);
        }
    }

    /**
     * 修改商品
     * @param item
     */
    @Override
    public void updateId(Item item) {
        //item.setUpdateTime(new Date());
        itemMapper.updateById(item);
    }

    /**
     * 根据ID删除
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        itemMapper.deleteById(id);
    }

    @Override
    public void culNum(Long itemId, Integer num) {
        Item item = itemMapper.selectById(itemId);
        //库存数量
        Integer stock = item.getStock();
        if (stock>=num)
        {
            //库存大于数量减输数量
            item.setStock(item.getStock()-num);
            itemMapper.updateById( item);
           // rabbitTemplate.convertAndSend(com.hmall.item.constants.MQConstants.EXCHANGE_NAME, com.hmall.item.constants.MQConstants.ROUTING_KEY_DELAY,itemId);
        }else
        {
            throw  new BaseException("库存不足！");
        }
    }
}
